<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 */
class Plugin_Sys extends Plugin
{
    function site_url(){
         $uri = $this->attribute('uri', 'String');    
         return site_url_lang($uri);
    }
    
	function canonical(){
	   /*
		$language_array = getLanguagesSite_ioc();
		 
		foreach(getLanguagesSite_ioc() as $id=>$lang){
			$segment = array();
			if(isset($_GET) AND !empty($_GET)){
				foreach($_GET as $k=>$val){
					if($k == 'l'){
						$segment[$k] = 'l='.$lang[1];
					}else{
						$segment[$k] = "$k=".$val;
					}
				}
			}
            
			if(!isset($segment['l'])){
				$segment['l'] = 'l='.get_language_code();
			}
			
			$tmp = baseURL();
			if('/' == $tmp[strlen($tmp)-1]){
				$url = baseURL().'?'.implode('&',$segment);
			}else{
				$url = baseURL().'/?'.implode('&',$segment);
			}
				
		}
        */
       	$ci= &get_instance();
        $url = fullURL();
        
        $check_is_escort = $ci->uri->segment(2);
        if($check_is_escort == 'escort'){
            $segs = $this->uri->segment_array();
	        $escort_id = $segs[count($segs)-1];
            //$escort_name =  $segs[count($segs)];
            //$url = site_url_lang("escort/$escort_id/$escort_name");
            $url = buildCustomProfileLink($escort_id);
        }
		return "<link rel=\"canonical\" href=\"$url\" />";
	}
	/**
	 * Usage:
	 * {{sys:translate_image name="image_name"}} |abc.jpg
	 *
	 * @param	string
	 * @return	string <img src="image_name_location" />
	 */
	function translate_image(){
		$name = $this->attribute('name', 'String'); 
		return imageGeoLanguage($name);
	}
	/**
	 * Usage:
	 * {{sys:translate slug="slug_here"}}
	 *
	 * @param	string
	 * @return	string
	 */
	function translate()
	{	
		$slug = $this->attribute('slug', 'String'); 
		
		return language_translate($slug);
	}
	
	function js_lang(){
		$lang = get_language_code();
		if(!is_file("./languages/$lang.js")){
			$file = 'en.js';
		}else{
			$file = "$lang.js";
		}
		return "<script type=\"text/javascript\" src=\"{{url:base}}languages/$file\"></script>";
	}
	
	function front_language(){
		/** $ci= &get_instance();
		if(isset($_GET['l'])){
			$l = 'en';
			foreach(getLanguagesSite_ioc() as $id=>$lang){
				if($lang[1] == $_GET['l']){
					$l = $_GET['l'];
					break;
				}
			}
			set_language_code($l);	
		
			setcookie("sys_lang", $l, time()+604800);
		}
		
		if(isset( $_COOKIE["sys_lang"] ) ){
			set_language_code($_COOKIE["sys_lang"]);
		}
		**/
		//return reDirectSite();
        $firt_segment = $this->uri->segment(1);
        if(!$firt_segment){
            redirect(site_url_lang('home'));
        }
	}
	
	function sys_reload(){
		if(isset($_GET['rl']) AND $_GET['rl'] == 1){
			$url = str_replace( array('&rl=1','?rl=1'), array('&rl=0','?rl=0'), fullURL() );
			return "<script type='text/javascript'>
				$(document).ready(function(){
					document.location='$url';
				});
			</script>";
			  
		}
	}
	
	function language(){
		$language_array = getLanguagesSite_ioc();
		$html = "<ul>";
		foreach(getLanguagesSite_ioc() as $id=>$lang){
			$current = get_language_id() == $id ? "class='active'":'';
			//$onclick = get_language_id() != $id ? "onclick='javascript:js_change_lang($id);'":'';
			//$link	 = get_language_id() != $id ? "<a href='javascript:void(0);' $onclick>".$lang[0]."</a>":$lang[0];
			     /**
				$segment = array();
				if(isset($_GET) AND !empty($_GET)){
					foreach($_GET as $k=>$val){
						if($k == 'l'){
							$segment[$k] = 'l='.$lang[1];
						}else{
							$segment[$k] = "$k=".$val;
						}
					}
				}else{
					$segment['l'] = 'l='.$lang[1];
				}
				if(!isset($segment['l'])){
					$segment['l'] = 'l='.$lang[1];
				}
				
				//$segment['rl'] = 'rl=1';

				$tmp = baseURL();
				if('/' == $tmp[strlen($tmp)-1]){
					$url = baseURL().'?'.implode('&',$segment);
				}else{
					$url = baseURL().'/?'.implode('&',$segment);
				}
				$link	 = get_language_id() != $id ? "<a href='$url'>".$lang[0]."</a>":$lang[0];
			     **/
                $current_lang_code = get_language_code();
                //$url = preg_replace( "/$current_lang_code/",'/'.$lang[1].'/',fullURL(),1 ); 
                $pos = strpos(fullURL(),"/$current_lang_code/");
                if ($pos !== false) {
                    $url = substr_replace(fullURL(),'/'.$lang[1].'/',$pos,strlen("/$current_lang_code/"));
                }else{
                    $url = fullURL();
                }
                
                $link	 = get_language_id() != $id ? "<a href='$url'>".$lang[0]."</a>":$lang[0];
                
			$html .= "<li $current>$link</li>";
		}
		$html .= "</ul>";
		$html .= "<script type='text/javascript'>
					function js_change_lang(id){
						$.post(BASE_URI+'home/change_language',{id:id},function(res){
							reload();
						});
					}
				</script>";
		return $html;
	} 
	
	function login(){
		if(isLogin()){
			$html = "<ul><li>".language_translate('plugin_sys_welcome')." ";
			$USER = getAccountUserObject();
			if(getPermission() == USER_TYPE_ESCORT){
				 $html .= "<a href='".site_url_lang('escorts/profile')."'>".$USER->getUserName()."</a>";
			}
			if(getPermission() == USER_TYPE_AGENT){
				$html .= "<a href='".site_url_lang('agent/my_profile')."'>".$USER->getUserName()."</a>";
			}	
			 
			$html .= "</li><li>";
			$html .= "<a href='".site_url_lang("authorization/logout")."'>".language_translate('top_link_logout')."</a></li></ul>"; 
		}else{
			$html = "<ul>
						<li><a href='".site_url_lang('registration')."'>".language_translate('top_link_register')."</a></li>
						<li><a href='".site_url_lang('authorization/login')."'>".language_translate('top_link_login')."</a></li>
					</ul>";
		}
		return $html;
	}
	
	function top_navigation(){
		$ci= &get_instance();
		
		$searchLink = site_url_lang('home/search?df=1');
		$escortLink = site_url_lang('home');
		//$mobileLink = site_url("page/announce");
		$contactLink = site_url_lang("home/contact");
		//$agentLink = site_url("page/agencia");
		$topEscortLink = site_url_lang('home/top_escorts');
		
		$newsLink = "";
		foreach($ci->escort_news_entity->getNewsByCategoryName('Top menu') as $item){
			$link = site_url_lang("page/{$item->slug}");
			$newsLink .= "<li><a href='$link'>".$item->title."</a></li>";
		}
		
		return "<ul id='top-navigation'>
					<li id='search-nav'><a href=\"$searchLink\">".language_translate('top_menu_search')."</a></li>
					<li id='escort-nav'><a href=\"$escortLink\">".language_translate('top_menu_escorts')."</a></li>
					<li id='top-escorts-nav'><a href=\"$topEscortLink\">".language_translate('top_menu_top_escorts')."</a></li>".
					//<li id='agent-nav'><a href=\"$agentLink\">".language_translate('top_menu_agent')."</a></li>
					//<li id='mobile-nav'><a href=\"$mobileLink\">".language_translate('top_menu_mobile')."</a></li>
					$newsLink
					."<li id='contact-nav'><a href=\"$contactLink\">".language_translate('top_menu_contact')."</a></li>
				</ul>";
	}
	
	function slogan(){
		return language_translate('site_slogan');
	}
	
	function search(){
		$ci= &get_instance();
		if(	
			$ci->module == 'escorts' OR 
			$ci->module == 'agent' OR 
			($ci->module == 'home' AND $ci->method == 'profile') OR	
			$ci->module == 'registration' OR
			$ci->module == 'authorization'
		)
		{
			return '<script type="text/javascript">$("#advanced-search").hide();</script>';
		}
		//array(5) { ["statusCode"]=> string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(14) "113.190.88.196" ["countryCode"]=> string(2) "VN" ["countryName"]=> string(8) "VIET NAM" } 
		//var_dump( $this->getCountryGeolocation() );
		$DEFAULT_COUNTRY = getCountryInfoFromUserGeoLocation();
		
		$ci->load->model('home/home_m');
		$pre = array_keys(getCountriesOfEscortsUsed());
		
		$preCountry = ($DEFAULT_COUNTRY) ? $DEFAULT_COUNTRY->getId() : $pre[0];
		
		$escortAgeRange = getEscortAgeRange();
		$minAge = 18;
		$maxAge = convertAgeFromTimeStamp($escortAgeRange['max_age']);
		  
		$priceRange = getPriceRange();
		$min_price = $priceRange['min_price'];
		$max_price = $priceRange['max_price'];
		
		$from_age = isset($_GET['from_age']) ? intval($_GET['from_age']) : $minAge;
		$to_age = isset($_GET['to_age']) ? intval($_GET['to_age']) :$maxAge;
		
		$from_price = isset($_GET['from_price']) ? intval($_GET['from_price']) : $min_price;
		$to_price = isset($_GET['to_price']) ? intval($_GET['to_price']) : $max_price;
		
		$category_id = (isset($_GET['category_id'])) ? intval($_GET['category_id']) : null;
		$country_id = (isset($_GET['country_id'])) ? intval($_GET['country_id']) : $preCountry;
		//$state_id = (isset($_GET['state_id'])) ? intval($_GET['state_id']) : null;
		//$province_id = (isset($_GET['province_id'])) ? intval($_GET['province_id']) : null;
		//$city_id = (isset($_GET['city_id'])) ? intval($_GET['city_id']) : null;
		
		//getEscortCategoryData_ioc()
		$categoryForm = form_dropdown('category_id', getSiteCategoryBaseOnSiteSetting(), array($category_id) , 'id="category_id_sys" class="custom"' );
		$countryForm = form_dropdown('country_id', getCountriesOfEscortsUsed(), array($country_id) , 'id="country_id_sys" class="custom"' );
		//$stateForm = form_dropdown('state_id', getStateData_ioc($country_id), array($state_id) , 'id="state_id_sys" class="custom"' );
		//$provinceForm = form_dropdown('province_id', getProvinceData_ioc($country_id,$state_id), array($province_id) , 'id="province_id_sys" class="custom"' );
		//$cityForm = form_dropdown('city_id', getCityData_ioc($country_id, $state_id, $province_id), array($city_id) , 'id="city_id_sys" class="custom"' );
		$stateForm = $ci->home_m->changeCountry_async($country_id);
		
		$name = (isset($_GET['name'])) ? $_GET['name'] : language_translate('plugin_sys_name');
		$search = language_translate('plugin_sys_search');
		
		$ageSlide = "<input type='hidden' name='from_age' id='sys_from_age' value='$from_age' />
			<input type='hidden' name='to_age' id='sys_to_age' value='$to_age' />
			<span class='sys-search-item-title'>".language_translate('search_escort_age')."</span>
			<div id='sys-slider-age' class='slider-horizontal'></div>
			<span id='sys_from_age_slide_show'>$from_age</span>
			<span id='sys_to_age_slide_show'>$to_age</span>
			";
			
		$priceSlide = "
			<input type='hidden' name='from_price' id='sys_from_price' value='$from_price' />
			<input type='hidden' name='to_price' id='sys_to_price' value='$to_price' />
			<span class='sys-search-item-title'>".language_translate('search_escort_price')." /&euro;</span>
			<div id='sys-slider-price' class='slider-horizontal'></div>
			<span id='sys_from_price_slide_show'>$from_price</span>
			<span id='sys_to_price_slide_show'>$to_price</span>
			";	
		
		return '<form action={{sys:site_url uri="home/search"}} id="sys_search_form" name="sys_search_form">
					
					<div class="name-input">
						<input type="text" name="name" value="'.$name.'" id="search_name"/>
					</div>
					<div class="category-input">
						'.$categoryForm.'
					</div>
					<div class="province-input">
						'.$countryForm.'
					</div>
					<div class="province-input">
						<div class="select-box" id="slb_state_id_sys" style="width:110px;float:none !important;">
						'.$stateForm.'</div>'.loader_image_s('id="slb_state_id_loader_sys" class="hidden" style="float:left;clear:both;"').'
					</div>
					<div class="province-input" style="position:relative;">'.
						//<div class="select-box" id="slb_province_id_sys" style="width:110px;float:none !important;">
						//'.$provinceForm.'</div>'.loader_image_s('id="slb_province_id_loader_sys" class="hidden" style="float:left;clear:both;"').'
						$ageSlide.
					'</div>
					<div class="ciutat-input" style="position:relative;">'.$priceSlide.
						//<div class="select-box" id="slb_city_id_sys" style="width:110px;float:none !important;">
						//'.$cityForm.'</div>'.loader_image_s('id="slb_city_id_loader_sys" class="hidden" style="float:left;clear:both;"').'
					'</div>
					<div class="search-button">
						<input type="submit" name="search" value="'.$search.'" />
					</div>
					<div class="clear"></div>
				</form>
				<script type="text/javascript">
					$(document).ready(function() {
						$("#search_name").focusin(function(){
							$name = "'.$name.'";
							if($(this).attr("value") == $name){
								$(this).attr("value","");
							}
						}); /* .focusout(function(){
							$name = "'.$name.'";
							if("" ==$(this).attr("value") ){
								$(this).attr("value",$name);
							}
						});	*/
						$("#sys_search_form").submit(function(){
							var name = "'.$name.'";
							if($("#search_name").val() == name){
								$("#search_name").attr("value","");
							}
							return true;
						});
					});
					
					function slideLabel(){
						$left1 = $("#sys-slider-age .ui-slider-handle").eq(0).css("left");
						$("#sys_from_age_slide_show").css({"left":$left1});
						$left2 = $("#sys-slider-age .ui-slider-handle").eq(1).css("left");
						$("#sys_to_age_slide_show").css({"left":$left2});
					}
					
					function slidePrice(){
						$left1 = $("#sys-slider-price .ui-slider-handle").eq(0).css("left");
						$("#sys_from_price_slide_show").css({"left":$left1});
						$left2 = $("#sys-slider-price .ui-slider-handle").eq(1).css("left");
						$("#sys_to_price_slide_show").css({"left":$left2});
					}
					
					$(document).ready(function() {
						$("#country_id_sys").live("change",function(){ 
							changeCountry_slb_sys($("#country_id_sys").val());
						});
						/*
							$("#state_id_sys").live("change",function(){
								changeState_slb_sys($("#state_id_sys").val(),$("#country_id_sys").val());
							});
							$("#province_id_sys").live("change",function(){
								changeProvince_slb_sys($("#state_id_sys").val(),$("#province_id_sys").val(),$("#country_id_sys").val());
							});
						*/
						
						$( "#sys-slider-age").slider({
							range: true,
							min: '.$minAge.',
							max: '.$maxAge.',
							values: ['.$from_age.','.$to_age.'],
							slide: function( event, ui ) {
								$( "#sys_from_age" ).attr(\'value\', ui.values[0]);
								$( "#sys_to_age" ).attr(\'value\', ui.values[1]);
								$( "#sys_from_age_slide_show" ).html(ui.values[0]);
								$( "#sys_to_age_slide_show" ).html(ui.values[1]);
								//slideLabel();
							},
							//change: function(event, ui) {slideLabel(); }
						});
						$( "#sys-slider-price").slider({
							range: true,
							min: '.$min_price.',
							max: '.$max_price.',
							values: ['.$from_price.','.$to_price.'],
							slide: function( event, ui ) {
								$( "#sys_from_price" ).attr(\'value\', ui.values[0]);
								$( "#sys_to_price" ).attr(\'value\', ui.values[1]);
								$( "#sys_from_price_slide_show" ).html(ui.values[0]);//+ \' &euro;\'
								$( "#sys_to_price_slide_show" ).html(ui.values[1]);//+ \' &euro;\'
								//slidePrice();
							},
							//change: function(event, ui) {slidePrice(); }
						});
						//slideLabel();
						//slidePrice();
					});	
					
					function changeCountry_slb_sys($country_id_sys){
						$("#slb_state_id_sys, #slb_province_id_sys, #slb_city_id_sys").html("<select class=\"custome\"></select>");
						$("select.custome").customStyle();
						$("#slb_state_id_loader_sys, #slb_province_id_loader_sys, #slb_city_id_loader_sys").show();
						$.post(BASE_URI+"home/changeCountry_async",{country_id:$country_id_sys},function(res){
							$("#slb_state_id_loader_sys").hide();
							$("#slb_state_id_sys").html(res);
							$("select#state_id_sys").customStyle();
							$firtElementValue = $("#state_id_sys").children().first().val();
							//changeState_slb_sys($firtElementValue,$country_id_sys);
						});
					}
					/**
						function changeState_slb_sys($state_id_sys,$country_id_sys){
							$("#slb_province_id_sys, #slb_city_id_sys").html("<select class=\"custome\"></select>");
							$("select.custome").customStyle();
							$("#slb_province_id_loader_sys, #slb_city_id_loader_sys").show();
							$.post(BASE_URI+"home/changeState_async",{state_id:$state_id_sys,country_id:$country_id_sys},function(res){
								$("#slb_province_id_loader_sys").hide();
								$("#slb_province_id_sys").html(res);
								$("select#province_id_sys").customStyle();
								$firtElementValue = $("#province_id_sys").children().first().val();
								changeProvince_slb_sys($state_id_sys,$firtElementValue,$country_id_sys);
							});
						}
						
						function changeProvince_slb_sys($state_id_sys,$province_id_sys,$country_id_sys){
							$("#slb_city_id_sys").html("<select class=\"custome\"></select>");
							$("select.custome").customStyle();
							$("#slb_city_id_loader_sys").show();
							$.post(BASE_URI+"home/changeProvince_async",{state_id:$state_id_sys,province_id:$province_id_sys,country_id:$country_id_sys},function(res){
								$("#slb_city_id_loader_sys").hide();
								$("#slb_city_id_sys").html(res);
								$("select#city_id_sys").customStyle();
							});
						}
					**/
				</script>
				';	
	}
	
	function breadcumb(){
		if(!isset($_SESSION['sys_breadcumb'])){
			$_SESSION['sys_breadcumb'][1] = array( language_translate('breadcumb_home') , site_url_lang('home') );
		}
		ksort($_SESSION['sys_breadcumb']); 
		$html = "<ul id='sys_bread_cumb'>";
		$size = count($_SESSION['sys_breadcumb']);
		$i = 0;
		foreach($_SESSION['sys_breadcumb'] as $item){
			if(!empty($item)){
				$i++ ;
				if( $i== $size){	
					$html .= "<li class='active'><a href='".$item[1]."'>".$item[0]."</a></li>";
				}else{
					$html .= "<li><a href='".$item[1]."'>".$item[0]."</a></li>";
				}	
			}	
		}
		$html .= "</ul>";
		//breadcumb_reset();
		
		$loaderImageCaptcha = loader_image();
		
		return "<script type='text/javascript'>
					$(document).ready(function(){
						$('#breadcumb').html(\"$html\");
					});
					var CAPTCHA_LOADER = \"{$loaderImageCaptcha}\";
				</script>";
	}
	
	function static_link(){
		
		if(isLogin()){
			if(getPermission() == USER_TYPE_ESCORT){
				return	'<ul>
							<li class="my-tab-link">
							<a href={{sys:site_url uri="escorts/myzone"}}>'.language_translate('plugin_sys_my_profile').'</a>
							</li>
						</ul>';
			}
			if(getPermission() == USER_TYPE_AGENT){
				return	'<ul>
							<li class="my-tab-link">
							<a href={{sys:site_url uri="agent/my_profile"}}>'.language_translate('plugin_sys_my_profile').'</a>
							</li>
						</ul>';
			}	
		}else{
			//breadcumb_reset();
			return '{{ widgets:area slug="top-static-link" }}';
		}		
	}
	
	function footer_left(){
		//{{url:base}}media/images/footer-img.png
		return '
				<div class="avd-image">
					'.imageGeoLanguage('footer-img.png').'
					<div class="footer-extra-link">'.language_translate('footer_extra_link').'</div>
				</div>
				<div class="trade-mark">
					'.language_translate('footer_copy_right').'
				</div>
			';
	}
	
	function footer_right_one(){
		return '<span class="header-column-text">Escorts por ciudades</span>
				<ul>
					<li><a href="">Escorts Madrid</a></li>
					<li><a href="">Escorts Barcelona</a></li>
					<li><a href="">Escorts Valencia</a></li>
					<li><a href="">Escorts Terrassa</a></li>
					<li><a href="">Escorts Sabadell</a></li>
					<li><a href="">Escorts España</a></li>
				</ul>';
	}
	
	function footer_right_two(){
		return '<span class="header-column-text">Webs Escorts</span>
				<ul>
					<li><a href="">Escorts Transexuales</a></li>
					<li class="active"><a href="">Escorts Hombres</a></li>
					<li><a href="">Escorts Mujeres</a></li>
				</ul>';
	}
	
	function footer_right_three(){
		return '<span class="header-column-text">Síguenos</span>
				<ul>
					<li class="facebook-link"><a href="">Facebook</a></li>
					<li class="twitter-link"><a href="">Twitter</a></li>
					<li class="vimeo-link"><a href="">Vimeo</a></li>
				</ul>';
	}
	
	function site_logo(){
		$logo = "logo_".SITE_REF.".png";
		if(! is_file("./images/logos/$logo")){
			$logo = "logo.png";
		}
		
		return "<a href='".site_url()."'><img src='".site_url()."images/logos/{$logo}' /></a>";
	}
	/*************************IP-GEOLOCATION*********************************/
	function getCountryGeolocation(){
		include_once('ip2locationlite.class.php');
 
		//Set geolocation cookie
		if(!isset($_COOKIE["geolocation_highescort"])){
		  $ipLite = new ip2location_lite;
		  $ipLite->setKey('06fc082707b16fc07af8082fdeceed43569eece7ad373b60b82a8870b0f555c6');
		 
		  $visitorGeolocation = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
		  if ($visitorGeolocation['statusCode'] == 'OK') {
			$data = base64_encode(serialize($visitorGeolocation));
			setcookie("geolocation_highescort", $data, time()+3600*24*7); //set cookie for 1 week
		  }
		}else{
		  $visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation_highescort"]));
		}
		return $visitorGeolocation;
	}
	
	//end
}

